//   @Name    : merchant_driver
//   @author  : 海参
//   @date    : 2022/3/22 19:01 AM
//   @desc    :自定义可以设置按钮大小的ElevatedButton；系统按钮有默认最小宽度/高度约束值Size(64pt,36pt),也有默认padding，
//   在遇到只需要高度小一点的ElevatedButton时就不适合用。如果设置按钮大小就固定大小，如果不设置则按系统按钮原理动态按内容调节。
//  2024.4.12 不要用opacity组件，防止离屏渲染.
//  2024.9.19 重构

import 'package:flutter/material.dart';

class CaiNoConstraintsElevatedButton extends StatelessWidget {
  ///点击回调
  final VoidCallback? onPressed;

  ///child
  final Widget? child;

  ///背景色
  final Color? backgroundColor;

  ///child前景色,文字，图标
  final Color? foregroundColor;
  final Color? disabledForegroundColor;
  final Color? disabledBackgroundColor;

  ///文本样式
  final TextStyle? textStyle;

  final EdgeInsetsGeometry? padding;

  ///按钮高度///按钮宽度
  final Size? fixedSize;

  ///边
  final BorderSide? side;

  ///边框样式,默认const RoundedRectangleBorder(borderRadius:BorderRadius.all(Radius.circular(5))),
  final OutlinedBorder? shape;

  ///半透明
  final double opacity;
  final ButtonStyle? style;

  const CaiNoConstraintsElevatedButton({
    super.key,
    required this.onPressed,
    required this.child,
    this.backgroundColor,
    this.foregroundColor,
    this.disabledForegroundColor,
    this.disabledBackgroundColor,
    this.textStyle,
    this.padding,
    this.fixedSize,
    this.side,
    this.shape,
    this.opacity = 0.7,
    this.style,
  });

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    double lcdX = size.width / 375;
    Color foregroundColor1 =
        foregroundColor ?? Theme.of(context).colorScheme.onPrimary;
    Color backgroundColor1 =
        backgroundColor ?? Theme.of(context).colorScheme.primary;

    final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
      foregroundColor: foregroundColor1,
      backgroundColor: backgroundColor1,
      disabledForegroundColor:
          disabledForegroundColor ?? foregroundColor1.withOpacity(opacity),
      disabledBackgroundColor:
          disabledBackgroundColor ?? backgroundColor1.withOpacity(opacity),
      elevation: 0,
      textStyle: textStyle,
      padding: padding ?? EdgeInsets.zero,
      fixedSize: fixedSize,
      side: side,
      shape: shape,
    );
    ElevatedButton elevatedButton = ElevatedButton(
      style: style ?? buttonStyle,
      onPressed: onPressed,
      child: child,
    );
    if (fixedSize != null) {
      final Size fixedSize1 = Size(64 * lcdX, 36 * lcdX);
      return SizedBox(
        height: (fixedSize!.height > 0 ? fixedSize!.height : fixedSize1.height),
        width: (fixedSize!.width > 0 ? fixedSize!.width : fixedSize1.width),
        child: elevatedButton,
      );
    }
    return elevatedButton;
  }
}
